#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-03-29 18:33:36
# @Author  : Gtlions Lai (gtlions.l@qq.com)
# @Link    : http://blog.csdn.net/gtlions
# @Version : 1.0.0
"""summarization ahout this script.

detail ahout this script

    Class(): summarization about Class
    ...
    function(): summarization about function
    ...
"""

import os
import sqlite3
from flask import Flask, request, url_for, g, render_template, session, redirect, flash
from flask.ext.sqlalchemy import SQLAlchemy
# from flask.ext.mail import Mail, Message
from threading import Thread
import config

sdb = sqlite3.connect("/Users/gtlions/gtlions/baidusync/laisync/10DevSpaces/pycharm/Misc/flask/contact/page/contact.db" ,check_same_thread=False)
cursor = sdb.cursor()

app = Flask(__name__)
# dbfile = os.path.join(app.instance_path, '../contact.db')
app.config[
    'SQLALCHEMY_DATABASE_URI'] = "sqlite:////Users/gtlions/gtlions/baidusync/laisync/10DevSpaces/pycharm/Misc/flask/contact/page/contact.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_ECHO'] = True
app.config.from_object(config)
app.secret_key = app.config['SECERT_KEY']
db = SQLAlchemy(app)
# mail = Mail(app)
from models import contact


@app.route("/")
@app.route("/index")
@app.route("/index/<int:page>", methods=['POST', 'GET'])
def index(page=1):
    data = contact.query.order_by(
        contact.username.desc()).paginate(page, 200, False)
    current = data.items
    return render_template("index.html", pagination=data, contactdata=current)


@app.route("/admin", methods=['POST', 'GET'])
def admin():
    if request.method == "POST":
        cursor.execute("select username,password from user where username=?", (request.form['email'],))
        user = cursor.fetchall()
        if not user:
            flash("invalid username", category='message')
            return render_template("login.html")
        elif request.form['pass'] != user[0][1]:
            flash("invalid password", category='message')
            return render_template("login.html")
        else:
            session['is_login'] = True
            session['username'] = request.form['email']
            return redirect(url_for("show"))
    else:
        return render_template("login.html")


@app.route("/admin/show")
@app.route("/admin/show/<int:page>")
def show(page=1):
    if session.has_key('is_login'):
        data = contact.query.order_by(
            contact.username.desc()).paginate(page, 10, False)
        current = data.items
        return render_template("list.html", pagination=data, contactdata=current)
    else:
        return redirect(url_for("admin"))


@app.route("/admin/add", methods=["POST", "GET"])
def add():
    if session.has_key('is_login') and session['username'] == app.config['USERNAME']:
        if request.method == "POST":
            if request.form['inlineRadioOptions'] == "option1":
                sex = "man"
            else:
                sex = "woman"
            con = contact(request.form['username'], request.form['year'], sex, request.form['job'], request.form['tel'],
                          request.form['qq'], request.form['email'])
            db.session.add(con)
            db.session.commit()
            flash("add success")
            return render_template('add.html')
        else:
            return render_template("add.html")
    else:
        return redirect(url_for("admin"))


@app.route("/admin/del/<int:page>")
def delete(page):
    if session.has_key('is_login') and session['username'] == app.config['USERNAME']:
        status = contact.query.filter(contact.id == page).first()
        if status == None:
            flash("delete failue")
            return redirect(url_for('show'))
        db.session.delete(status)
        db.session.commit()
        flash("delete success")
        return redirect(url_for('show'))
    else:
        return redirect(url_for('admin'))


@app.route("/admin/update/<int:page>", methods=["POST", "GET"])
def update(page):
    if session.has_key('is_login') and session['username'] == app.config['USERNAME']:
        if request.method == "POST":
            if request.form['inlineRadioOptions'] == "option1":
                sex = "男"
            else:
                sex = "女"
            con = contact.query.get(page)
            # return con.id
            if con == None:
                flash("update error")
                return redirect(url_for("show"))
            con.username = request.form['username']
            con.year = request.form['year']
            con.sex = sex
            con.job = request.form['job']
            con.tel = request.form['tel']
            con.qq = request.form['qq']
            con.email = request.form['email']
            db.session.commit()
            flash("update success")
            return redirect(url_for("show"))
        else:
            contactdata = contact.query.get(page)
            if contactdata == None:
                return render_template("update.html")
            return render_template("update.html", data=contactdata)
    else:
        return render_template(url_for("admin"))


@app.route("/logout")
def logout():
    session.pop('is_login', None)
    session.pop('username', None)
    flash("You were log out")
    return redirect(url_for('admin'))
